package com.lzw.linspace.controller;

import com.lzw.linspace.po.FileInfoPO;
import com.lzw.linspace.service.UploadService;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 文件接口
 */
@RestController
@RequestMapping("/file")
public class FileController {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class);

    private UploadService uploadService;

    @Autowired
    public void setUploadService(UploadService uploadService) {
        this.uploadService = uploadService;
    }

    /**
     * 上传单个文件
     *
     * @param mFile    文件
     * @param filePath 文件路径
     */
    @SneakyThrows
    @RequestMapping(value = "/uploadSingleFile", method = RequestMethod.POST)
    public void uploadSingleFile(MultipartFile mFile, String filePath) {
        uploadService.uploadSingleFile(mFile, filePath);
        LOGGER.info("上传文件成功");
    }

    /**
     * 根据文件id删除
     *
     * @param id 文件id
     */
    @RequestMapping(value = "/deleteSingleFileById", method = RequestMethod.PUT)
    public void deleteSingleFileById(Long id) {
        uploadService.deleteSingleFileById(id);
        LOGGER.info("删除文件成功");
    }

    /**
     * 根据文件id修改文件名
     *
     * @param id           文件id
     * @param fileFullName 文件名全称（包括文件后缀）
     */
    @RequestMapping(value = "/modifyFileNameById", method = RequestMethod.PUT)
    public void modifyFileNameById(Long id, String fileFullName) {
        uploadService.modifyFileNameById(id, fileFullName);
        LOGGER.info("修改文件名成功");
    }

    /**
     * 根据文件id修改文件路径
     *
     * @param id       文件id
     * @param filePath 文件路径
     */
    @RequestMapping(value = "/modifyFilePathById", method = RequestMethod.PUT)
    public void modifyFilePathById(Long id, String filePath) {
        uploadService.modifyFilePathById(id, filePath);
        LOGGER.info("修改文件路径成功");
    }

    /**
     * 获取文件信息列表
     *
     * @param fileInfoPO 文件信息
     * @return 文件信息列表
     */
    @RequestMapping(value = "/listFileInfoPO", method = RequestMethod.GET)
    public List<FileInfoPO> listFileInfoPO(FileInfoPO fileInfoPO) {
        return uploadService.listFileInfoPO(fileInfoPO);
    }
}
